package com.cosina.game.crystallight.ui.menu.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;

import com.cosina.game.crystallight.ui.common.PaintSuite;
import com.cosina.game.crystallight.ui.menu.CompositeView;
import com.cosina.game.crystallight.ui.menu.transform.TransformSuite;
import com.cosina.game.crystallight.util.CanvasUtil;

public class ViewPart {
	
	protected final int id;
	protected final Rect occpuy;
	public Bitmap image;
	public String text;
	public int textColor = Color.WHITE;
	protected CompositeView belongView;
	
	private TransformSuite ts = TransformSuite.NULLSUITE;
	
	public ViewPart(int id,CompositeView belongView,Bitmap image,Point leftTop){
		this(id,belongView,image,leftTop,TransformSuite.NULLSUITE);
	}
	
	public ViewPart(int id,CompositeView belongView,Bitmap image,Point leftTop,TransformSuite ts){
		this.id = id;
		this.belongView = belongView;
		this.image = image;
		this.occpuy = new Rect(leftTop.x,leftTop.y,leftTop.x + image.getWidth(), leftTop.y+ image.getHeight());
		this.ts = ts;
		this.ts.setOccpuy(occpuy);
	}
	
	public boolean containPoint(int x,int y){
		return occpuy.contains(x,y);
	}
	
	public final int getId(){
		return id;
	}

	
	public void click(){
		ts.buttonClick();
		belongView.postDelayed(new Runnable(){public void run(){belongView.onButtonClicked(id);}}, 300);
	}
	
	public void draw(Canvas canvas) {
		ts.undoT(canvas);
		canvas.drawBitmap(image, occpuy.left,occpuy.top, PaintSuite.p4Simple);
		if(null != text && "".equals(text) == false){
			int colorBack = PaintSuite.p4Text.getColor();
			PaintSuite.p4Text.setColor(textColor);
			CanvasUtil.drawText(canvas, text, occpuy.left + 28, occpuy.top + 27, PaintSuite.p4Text, 16);
			PaintSuite.p4Text.setColor(colorBack);
		}
		ts.comeBack(canvas);
	}
}
